import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/views/layout'
import Login from '@/views/login'
import Home from '@/views/home'
import Register from '@/views/register'
import ArticleClassify from '@/views/article/articleClassify'
import ArticleList from '@/views/article/articleList'
import AddArticle from '@/views/article/addArticle'
import Userinfo from '@/views/user/userinfo'
import Avator from '@/views/user/avator'
import UpdatePwd from '@/views/user/updatePwd'
import store from '@/store'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    { path: '/register', component: Register },
    {
      path: '/',
      component: Layout,
      redirect: '/home',
      children: [
        { path: '/home', component: Home },
        { path: '/art-cate', component: ArticleClassify },
        { path: '/art-list', component: ArticleList },
        { path: '/addArticle', component: AddArticle },
        { path: '/user-info', component: Userinfo },
        { path: '/user-avatar', component: Avator },
        { path: '/user-pwd', component: UpdatePwd }
      ]
    }
  ]
})

// 白名单
const whiteList = ['/login', '/register']

// 路由导航守卫
router.beforeEach((to, from, next) => {
  const token = store.state.user.token
  if (token) {
    next()
    return
  }
  if (whiteList.includes(to.path)) {
    next()
    return
  }
  next('/login')
})

export default router
